package com.neighbor.neighborhooduser.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neighbor.neighborhooduser.pojo.MallProducts;
import com.neighbor.neighborhooduser.service.IMallProductsService;
import com.neighbor.neighborhooduser.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 积分商城商品表 前端控制器
 * </p>
 *
 * @author song
 * @since 2025-09-22 09:13:04
 */
@RestController
@RequestMapping("/mall")
public class MallProductsController {
    @Autowired
    IMallProductsService mallProductsService;
    // 得到商品列表
    @GetMapping("products")
    ResponseMsg productsList(){
        IPage<MallProducts> pageInfo = new Page<>();
        QueryWrapper<MallProducts> wrapper = new QueryWrapper<>();
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "获取成功", mallProductsService.list(pageInfo, wrapper));
    }
    // 得到单个商品
    @GetMapping("/products/{productId}")
    ResponseMsg getOneProducts(@PathVariable Integer productId){
        System.out.println("商品Id: " + productId);
        QueryWrapper<MallProducts> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", productId);
        MallProducts mallProducts = mallProductsService.getOne(wrapper);
        if (null == mallProducts){
            return ResponseMsg.getInstance(ResponseMsg.PARAMETER_ERROR, "该商品不存在");
        }
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "查找成功", mallProducts);
    }
    // 根据条件查询商品数据
    @RequestMapping("products/getProducts")
    ResponseMsg getProducts(@RequestBody Map<String, String> params){
         Integer typeId = Integer.valueOf(params.getOrDefault("productsType", ""));
        String nickname = params.getOrDefault("name", null);
         Integer sort = Integer.valueOf(params.getOrDefault("pointsSort", "1"));
        Integer pointsScope = Integer.valueOf(params.getOrDefault("pointsScope", "0"));
        Integer low = 0;
        Integer high = 10000;
        if (pointsScope == 1){
            high = 500;
        }else if (pointsScope == 2){
            low = 500;
            high = 1000;
        } else if (pointsScope == 3) {
            low = 1000;
        }
        Map<String, Object> p = new HashMap<>();
        p.put("typeId", typeId);
        p.put("nickname", nickname);
        p.put("low", low);
        p.put("high", high);
        p.put("sort", sort);
        List<MallProducts> list = mallProductsService.getMallProductsByParams(p);
        if ( list == null){
            return ResponseMsg.getInstance(ResponseMsg.PARAMETER_ERROR, "没有数据");
        }
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "查询成功", list);
    }

    /**
     * 修改商品 信息
     * @param mallProducts
     * @return
     */
    @RequestMapping("products/updateProducts")
    ResponseMsg updateProducts(@RequestBody MallProducts mallProducts){
        UpdateWrapper<MallProducts> wrapper = new UpdateWrapper<>();
        wrapper.set(mallProducts.getTypeId() != null, "type_id", mallProducts.getTypeId())
                .set(mallProducts.getName() != null, "name", mallProducts.getName())
                .set(mallProducts.getDescription() != null, "description", mallProducts.getDescription())
                .set(mallProducts.getImages() != null, "images", mallProducts.getImages())
                .set(mallProducts.getPointsPrice() != null, "points_price", mallProducts.getPointsPrice())
                .set(mallProducts.getStock() != null, "stock", mallProducts.getStock())
                .set(mallProducts.getStatus() != null, "status", mallProducts.getStatus())
                .set(mallProducts.getCreatedAt() != null, "created_at", mallProducts.getCreatedAt())
                .set(mallProducts.getUpdatedAt() != null, "updated_at", mallProducts.getUpdatedAt())
                .eq(mallProducts.getProductId() != null, "product_id", mallProducts.getProductId());
        if (mallProductsService.update(wrapper)) {
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "修改成功", mallProducts);
        }
        return ResponseMsg.getInstance(ResponseMsg.PARAMETER_ERROR, "修改失败");
    }
}
